#!/usr/bin/python
# coding=utf-8
import os
import pickle
from ConfigParser import ConfigParser


def main():

    CONFIGFILE = "config.txt"
    config = ConfigParser()
    config.read(CONFIGFILE)

    values = dict()
    counts = dict()
    data_extract(config, values, counts)

    store(values, 'values.pkl')
    store(counts, 'counts.pkl')

    format1 = "%s -> [%d] "
    format2 = "(%s : %s)"
    f = open(config.get('Filename', 'output2'), 'w')
    for node1 in values:
        print >> f, format1 % (node1, len(values[node1])),
        for node2 in values[node1]:
            dis = values[node1][node2][1]
            value = float(pow(values[node1][node2][0], 2)) / (counts[node1] * counts[node2] * pow(dis, 2))
            print >> f, format2 % (node2, value),
        print >> f
    f.close()


def data_extract(config, values, counts):

    index = 1
    for file in os.listdir(config.get('Filename', 'dir')):
        if os.path.isfile(file) and file.startswith(config.get('Filename', 'prefix')):
            fi = open(file)
            while True:
                lines = fi.readlines(10000)
                if not lines:
                    break
                for line in lines:
                    if index % 1000 == 0:
                        print index
                    index += 1
                    Nodes = dict()
                    nodes = line.strip().split(' ')
                    nodes.pop()
                    for i, n in enumerate(nodes):

                        if n in Nodes:
                            Nodes[n].append(i)
                        else:
                            Nodes[n] = [i]

                    for i in Nodes:
                        if i in counts:
                            counts[i] += 1
                        else:
                            counts[i] = 1

                        for j in Nodes:
                            if i == j:
                                continue
                            dis = []
                            for i_index in Nodes[i]:
                                for j_index in Nodes[j]:
                                    i2 = int(i_index)
                                    j2 = int(j_index)
                                    if i2 < j2:
                                        dis.append(j2 - i2)
                            if len(dis):
                                min_dis = min(dis)
                                if i in values:
                                    if j in values[i]:
                                        if values[i][j][1] > min_dis:
                                            values[i][j][1] = min_dis
                                        values[i][j][0] += 1
                                    else:
                                        values[i][j]= [1,min_dis]

                                else:
                                    values[i] = {j:[1,min_dis]}

            fi.close()
    try:
        os.system("rm " + config.get('Filename', 'dir') + "/" + config.get('Filename', 'prefix') + "*")
    finally:
        pass


def store(data, tmp_filename):
    output1 = open(tmp_filename,'wb')
    pickle.dump(data, output1)
    output1.close()


if __name__ == '__main__':
    main()
